package com.doitdroid;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class TaskActivity extends ListActivity {

	private static final String[] PROJECTION = new String[] {
			TaskInfo.Task._ID, TaskInfo.Task.NAME, TaskInfo.Task.DUE_DATE,TaskInfo.Task.LIST_ID,};

	private final int MENU_ADD = 1, MENU_EDIT = 2, MENU_DELETE = 3;
	private final int GROUP_DEFAULT = 0, GROUP_DEL = 1;

	private boolean isDeleting = false;

	private void setIsDeleting(boolean isDeleting) {
		this.isDeleting = isDeleting;
	}

	private boolean getIsDeleting() {
		return isDeleting;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// If no data was given in the intent (because we were started
		// as a MAIN activity), then use our default content provider.
		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(TaskInfo.Task.CONTENT_URI);
		}

		Cursor cursor = managedQuery(intent.getData(), PROJECTION, null, null,
				TaskInfo.Task.DEFAULT_SORT_ORDER);

		// Used to map notes entries from the database to views
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.taskitemtemplate, cursor,
				new String[] { TaskInfo.Task.NAME, TaskInfo.Task.DUE_DATE },
				new int[] { R.id.taskItemTemplateNameText, R.id.taskItemTemplateDueText,  });
		setListAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(GROUP_DEFAULT, MENU_ADD, 0, "Add");
		menu.add(GROUP_DEFAULT, MENU_EDIT, 0, "Edit");
		menu.add(GROUP_DEL, MENU_DELETE, 0, "Delete");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_ADD:
			this.CreateListItem();
			return true;
		case MENU_EDIT:
			this.StartListItemEdition();
			return true;
		case MENU_DELETE:
			this.StartListItemDeletion();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		if (this.getIsDeleting())
			this.DeleteListItem(id);
		else 
			this.EditListItem(id);
	}

	private void StartListItemDeletion() {
		this.setIsDeleting(true);

		Toast.makeText(this, "Select Item for delete", Toast.LENGTH_SHORT)
				.show();
	}

	private void StartListItemEdition() {
		this.setIsDeleting(false);

		Toast.makeText(this, "Select Item for edit", Toast.LENGTH_SHORT).show();
	}

	private void DeleteListItem(long id) {
		Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), id);
		getContentResolver().delete(noteUri, null, null);
	}

	private void EditListItem(long id) {
		Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);

		String action = getIntent().getAction();
		if (Intent.ACTION_PICK.equals(action)
				|| Intent.ACTION_GET_CONTENT.equals(action)) {
			// The caller is waiting for us to return a note selected by
			// the user. The have clicked on one, so return it now.
			setResult(RESULT_OK, new Intent().setData(uri));
		} else {
			// Launch activity to view/edit the currently selected item
			startActivity(new Intent(Intent.ACTION_EDIT, uri));
		}
	}

	private void CreateListItem() {
		startActivity(new Intent("android.intent.action.INSERT", getIntent()
				.getData()));
	}
}